﻿using System.Collections.Generic;
using System.Linq;

namespace StudyEnglish.Application.Filter
{
    public abstract class BaseFilter
    {
        #region properties
        public const int NumberShift = 10;

        public string InitialUrl;

        private string BaseUrl
        {
            get { return InitialUrl + "&filter="; }
        }

        


        public abstract IList<BaseFilterCategory> Categories { get; }
        
       
        /// <summary>
        /// Returns all selected items
        /// </summary>
        public IList<BaseFilterItem> SelectedItems
        {
            get
            {
                var result = new List<BaseFilterItem>();
                foreach (var cat in Categories)
                {
                    result.AddRange(cat.SelectedItems);
                }
                return result;
            }
        }

        #endregion




        public bool IsActive()
        {
            return Categories.Count > 0;
        }

        /// <summary>
        /// Builds 
        /// </summary>
        /// <returns></returns>
        public string GetCurrentFilterUrlString()
        {
            var url = string.Empty;
            foreach (var selectedItem in SelectedItems)
            {
                url += selectedItem.GetFullItemKey() + "-";
            }
            return url;
        }

        /// <summary>
        /// Applies to the current filter request's filter
        /// </summary>
        /// <param name="requestFilterString"></param>
        public void ApplyRequestFilter(string requestFilterString)
        {
            if (!string.IsNullOrEmpty(requestFilterString))
            {
                var requestItems = requestFilterString.Split('-');
                foreach (var requestItem in requestItems)
                {
                    var item = GetItemByKey(requestItem);
                    if (item != null)
                    {
                        item.IsSelected = true;
                    }
                }
            }
        }

        private BaseFilterItem GetItemByKey(string requestItem)
        {
            BaseFilterItem result = null;

            if (requestItem.Length == 4)
            {
                var catKey = requestItem.Substring(0, 2);
                var filterItemKey = requestItem.Substring(2, 2);

                int catIntKey;
                int itemIntKey;
                if (int.TryParse(catKey, out catIntKey) && int.TryParse(filterItemKey, out itemIntKey))
                {
                    catIntKey = catIntKey - NumberShift;
                    itemIntKey = itemIntKey - NumberShift;
                    if (catIntKey >= 0 && catIntKey < Categories.Count())
                    {
                        var category = Categories[catIntKey];
                        if (itemIntKey >= 0 && itemIntKey < category.Items.Count)
                        {
                            result = category.Items[itemIntKey];
                        }
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// Get's url for adding new filter
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public string GetUrlForAddingItem(BaseFilterItem item)
        {
            var url = GetCurrentFilterUrlString();
            if (item.IsSelected == false)
            {
                url = url + item.GetFullItemKey();
            }
            return BaseUrl + url;
        }

        /// <summary>
        /// Return url for removing item
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public string GetUrlForRemovingItem(BaseFilterItem item)
        {
            var url = BaseUrl;
            foreach (var selectedItem in SelectedItems)
            {
                if (selectedItem.GetFullItemKey() != item.GetFullItemKey())
                {
                    url += selectedItem.GetFullItemKey() + "-";
                }
            }
            return url;
        }
    }
}